//
//  HHSelectUserTableViewCell.m
//  HHChatKit
//
//  Created by Henry on 2021/8/17.
//

#import "HHSelectUserTableViewCell.h"
#import <HHTool/HHTool.h>
#import "HHChatConfiguration.h"
#import "UIImageView+HHChat.h"
#import "HHResources.h"

@interface HHSelectUserTableViewCell ()

@property (nonatomic, strong) UIImageView *selectImageView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation HHSelectUserTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _selectImageView = [[UIImageView alloc] init];
    _iconImageView = [[UIImageView alloc] init];
    _nameLabel = [[UILabel alloc] init];
    
    [self.contentView addSubview:_selectImageView];
    [self.contentView addSubview:_iconImageView];
    [self.contentView addSubview:_nameLabel];
    
    [_iconImageView hc_setCornerRadius];
    [self setupFont];
}

- (void)setupFont {
    _nameLabel.font = [HHChatConfiguration shareInstance].defaultFont;
}

- (void)setModel:(HHSelectUserModel *)model {
    _model = model;
    _nameLabel.text = model.name ?: @"";
    
    if (model.portraitImage) {
        _iconImageView.image = model.portraitImage;
    } else {
        [_iconImageView hh_setAvatarWithUrl:model.portrait ID:model.userId name:model.name];
    }
    
    self.checked = model.isSelected;
    
    self.unable = model.unable;
}

- (void)setChecked:(BOOL)checked {
    _checked = checked;
    if (checked) {
        _selectImageView.image = [HHResources getImageFromMessage:@"icon_selected"];
    } else {
        _selectImageView.image = [HHResources getImageFromMessage:@"icon_selected_none"];
    }
}

- (void)setUnable:(BOOL)unable {
    _unable = unable;
    if (_unable) {
//        _selectImageView.image = [HHResources getImageFromMessage:@"icon_selected_unable"];
        _selectImageView.alpha = 0.5;
        _iconImageView.alpha = 0.5;
        _nameLabel.alpha = 0.5;
    } else {
        _selectImageView.alpha = 1;
        _iconImageView.alpha = 1;
        _nameLabel.alpha = 1;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _selectImageView.frame = CGRectMake(20, self.height/2-12, 24, 24);
    _iconImageView.frame = CGRectMake(_selectImageView.right + 12, 12, self.height-24, self.height-24);
    _nameLabel.frame = CGRectMake(_iconImageView.right + 12, 0, self.width-_iconImageView.right-40, self.height);
}

+ (CGFloat)viewHeight {
    CGFloat height = [HHChatConfiguration shareInstance].defaultFont.lineHeight + 44;
    return ceil(height);
}

@end
